package com.jeecg.qywx.api.user;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jeecg.qywx.api.base.JwAccessTokenAPI;
import com.jeecg.qywx.api.base.JwParamesAPI;
import com.jeecg.qywx.api.core.common.AccessToken;
import com.jeecg.qywx.api.core.util.HttpUtil;
import com.jeecg.qywx.api.user.vo.Convert2OpenidReq;
import com.jeecg.qywx.api.user.vo.Convert2OpenidRes;
import com.jeecg.qywx.api.user.vo.Convert2UseridReq;
import com.jeecg.qywx.api.user.vo.Convert2UseridRes;
import com.jeecg.qywx.api.user.vo.UserDetail;
import com.jeecg.qywx.api.user.vo.UserInfo;

/**
 * 企业微信--menu
 * 
 * @author zhoujf
 * 
 */
public class JwUserAPI {

	/** 1 创建成员 */
	private static String user_create_url = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN";
	/** 2 更新成员 */
	private static String user_update_url = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=ACCESS_TOKEN";
	/** 3 删除成员 */
	private static String user_delete_url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=USERID";
	/** 4 批量删除成员 */
	private static String user_delete_all_url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token=ACCESS_TOKEN";
	/** 5 获取成员 */
	private static String user_get_url_byuserid = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
	/** 6 获取部门下的成员 */
	private static String user_get_dep_all_url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD&status=STATUS";
	/** 7 获取部门成员(详情) */
	private static String user_get_dep_all_detail_url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD&status=STATUS";
	/** userid转换成openid接口 */
	private static String convert_to_openid_url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=ACCESS_TOKEN";
	/** openid转换成userid接口 */
	private static String convert_to_userid_url = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid?access_token=ACCESS_TOKEN";

	/**
	 * 根据openid获取成员的userid
	 * 
	 * @param userid
	 *            企业号内的成员id
	 * @param agentid
	 *            整型，需要发送红包的应用ID，若只是使用微信支付和企业转账，则无需该参数
	 * @param STATUS
	 * @return
	 */
	public static Convert2UseridRes convert2Userid(Convert2UseridReq convert2UseridReq) {
		AccessToken accessToken = JwAccessTokenAPI.getAccessToken(JwParamesAPI.corpId, JwParamesAPI.secret);
		String url = convert_to_userid_url.replace("ACCESS_TOKEN", accessToken.getAccesstoken());
		String jsonParam = JSONObject.toJSONString(convert2UseridReq);
		JSONObject jsonObject = HttpUtil.sendPost(url, jsonParam);
		if (!jsonObject.containsKey("errcode")) {
			return null;
		}
		if (!"0".equals(jsonObject.getString("errcode"))) {
			return null;
		}
		Gson gson = new Gson();
		Convert2UseridRes ps = gson.fromJson(jsonObject.toString(), Convert2UseridRes.class);
		return ps;
	}
	
	/**
	 * 根据user_id获取成员openid
	 * 
	 * @param userid
	 *            企业号内的成员id
	 * @param agentid
	 *            整型，需要发送红包的应用ID，若只是使用微信支付和企业转账，则无需该参数
	 * @param STATUS
	 * @return
	 */
	public static Convert2OpenidRes convert2Openid(Convert2OpenidReq convert2Openid) {
		AccessToken accessToken = JwAccessTokenAPI.getAccessToken(JwParamesAPI.corpId, JwParamesAPI.secret);
		String url = convert_to_openid_url.replace("ACCESS_TOKEN", accessToken.getAccesstoken());
		String jsonParam = JSONObject.toJSONString(convert2Openid);
		JSONObject jsonObject = HttpUtil.sendPost(url, jsonParam);
		if (!jsonObject.containsKey("errcode")) {
			return null;
		}
		if (!"0".equals(jsonObject.getString("errcode"))) {
			return null;
		}
		Gson gson = new Gson();
		Convert2OpenidRes ps = gson.fromJson(jsonObject.toString(), Convert2OpenidRes.class);
		return ps;
	}

	/**
	 * 根据userid获取用户信息详情
	 * 
	 * @param userid
	 * @return
	 */
	public static UserDetail userGetUrlByUserid(String userid) {
		AccessToken accessToken = JwAccessTokenAPI.getAccessToken(JwParamesAPI.corpId, JwParamesAPI.secret);
		String url = user_get_url_byuserid.replace("ACCESS_TOKEN", accessToken.getAccesstoken()).replace("USERID",
				userid);
		JSONObject jsonObject = HttpUtil.sendGet(url);
		if (!jsonObject.containsKey("errcode")) {
			return null;
		}
		if (!"0".equals(jsonObject.getString("errcode"))) {
			return null;
		}
		Gson gson = new Gson();
		UserDetail ps = gson.fromJson(jsonObject.toString(), UserDetail.class);
		return ps;
	}

	/**
	 * 获取部门下的成员
	 * 
	 * @param DEPARTMENT_ID 获取的部门id
	 * @param FETCH_CHILD 1/0：是否递归获取子部门下面的成员
	 * @param STATUS 0获取全部成员，1获取已关注成员列表，2获取禁用成员列表，4获取未关注成员列表。status可叠加，未填写则默认为4
	 * @return
	 */
	public static List<UserInfo> userGetDepAll(String DEPARTMENT_ID, String STATUS) {
		List<UserInfo> list = new ArrayList<UserInfo>(); 
		AccessToken accessToken = JwAccessTokenAPI.getAccessToken(JwParamesAPI.corpId, JwParamesAPI.secret);
		String url = user_get_dep_all_url.replace("ACCESS_TOKEN", accessToken.getAccesstoken())
				.replace("DEPARTMENT_ID", DEPARTMENT_ID).replace("FETCH_CHILD", "1").replace("STATUS", STATUS);
		JSONObject jsonObject = HttpUtil.sendGet(url);
		if (!jsonObject.containsKey("errcode")) {
			return list;
		}
		if (!"0".equals(jsonObject.getString("errcode"))) {
			return list;
		}
		if (!jsonObject.containsKey("userlist")) {
			return list;
		}
		Gson gson = new Gson();
		list = gson.fromJson(jsonObject.getString("userlist"),  new TypeToken<List<UserInfo>>(){}.getType());
		return list;
	}

	/**
	 * 获取部门成员(详情)
	 * 
	 * @param DEPARTMENT_ID 部门id
	 * @param FETCH_CHILD 
	 * @param STATUS
	 * @return
	 */
	public static List<UserDetail> userGetDetail(String DEPARTMENT_ID, String STATUS) {
		// 拼装获取菜单列表的url
		List<UserDetail> list = new ArrayList<UserDetail>();
		AccessToken accessToken = JwAccessTokenAPI.getAccessToken(JwParamesAPI.corpId, JwParamesAPI.secret);
		String url = user_get_dep_all_detail_url.replace("ACCESS_TOKEN", accessToken.getAccesstoken())
				.replace("DEPARTMENT_ID", DEPARTMENT_ID).replace("FETCH_CHILD", "1").replace("STATUS", STATUS);
		// 调用接口获取菜单列表
		JSONObject jsonObject = HttpUtil.sendGet(url);
		if (!jsonObject.containsKey("errcode")) {
			return list;
		}
		if (!"0".equals(jsonObject.getString("errcode"))) {
			return list;
		}
		if (!jsonObject.containsKey("userlist")) {
			return list;
		}
		Gson gson = new Gson();
		list = gson.fromJson(jsonObject.getString("userlist"),  new TypeToken<List<UserDetail>>(){}.getType());
		return list;
	}

	public static void main(String[] args) {
		AccessToken accessToken = JwAccessTokenAPI.getAccessToken(JwParamesAPI.corpId, JwParamesAPI.secret);
		String DEPARTMENT_ID = "1";
		String STATUS = "1";
		System.out.println(accessToken.getAccesstoken());
		System.out.println(userGetDepAll(DEPARTMENT_ID, STATUS));
		System.out.println(userGetDetail(DEPARTMENT_ID, STATUS));
	}

}
